package algorithm.leetcode.I1to100;

import java.util.*;

/**
 * 思路: 将每个单词按其字母升序排列做一个变换得到其key,本身为value放到map中
 */

public class Q49 {

    public List<List<String>> groupAnagrams(String[] strs) {
        HashMap<String, List<String>> map = new HashMap<>();
        List<List<String>> res = new LinkedList<>();

        for (String str : strs) {
            char[] chars = str.toCharArray();
            Arrays.sort(chars);
            String newStr = new String(chars);
            if (map.containsKey(newStr)) map.get(newStr).add(str);
            else map.put(newStr, new LinkedList<>(Arrays.asList(str)));
        }

        map.entrySet().stream().map(Map.Entry::getValue).forEach(res::add);
        return res;
    }

}
